package cn.tedu.eguser1.controller;

import cn.tedu.eguser1.base.response.JsonResult;
import cn.tedu.eguser1.base.response.StatusCode;
import cn.tedu.eguser1.mapper.UserMapper;
import cn.tedu.eguser1.pojo.dto.UserAddParam;
import cn.tedu.eguser1.pojo.dto.UserUpdateParam;
import cn.tedu.eguser1.pojo.entity.User;
import cn.tedu.eguser1.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Tag(name = "用户管理模块")
@Slf4j
@RestController
@RequestMapping("v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    /**
     * 1.添加用户功能
     */
    @RequestMapping("add")
    @Operation(summary = "1.添加用户")
    @ApiOperationSupport(order = 10)
    @Parameters(value = {
            @Parameter(name = "username",description = "用户名",required = true),
            @Parameter(name = "password",description = "密码",required = true),
            @Parameter(name = "nickname",description = "昵称",required = true),
            @Parameter(name = "userAddParam",hidden = true)
    })
    public JsonResult add(UserAddParam userAddParam){
       log.debug("userAddParam="+userAddParam);
        User user = new User();
        BeanUtils.copyProperties(userAddParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insertUser(user);
        return JsonResult.ok(user);
    }

    /**
     * 2.查询用户列表功能
     */
    @GetMapping("list")
    @Operation(summary = "2.用户列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(){
        List<UserListVO> userListVOList = userMapper.selectUserList();
        log.debug("userListVOList="+userListVOList);
        return JsonResult.ok(userListVOList);
    }

    /**
     * 3.删除用户功能
     */
    @DeleteMapping("del")
    @Operation(summary = "3.删除用户")
    @ApiOperationSupport(order = 30)
    @Parameter(name = "id",description = "用户id",required = true,hidden = true)
    public JsonResult del(Long id){
        log.debug("id="+id);
        int num = userMapper.deleteUser(id);
        if(num >  0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    /**
     * 4.更新用户功能
     */
    @PostMapping("update")
    @Operation(summary = "4.更新用户")
    @ApiOperationSupport(order = 40)
    @Parameters(value = {
            @Parameter(name = "id",description = "用户id",required = true),
            @Parameter(name = "username",description = "用户名",required = true),
            @Parameter(name = "password",description = "密码",required = true),
            @Parameter(name = "nickname",description = "昵称",required = true),
            @Parameter(name = "userUpdateParam",hidden = true)
    })
    public JsonResult UpdateUser(UserUpdateParam userUpdateParam){
        log.debug("userUpdateParam="+  userUpdateParam);
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        user.setUpdateTime(new Date());
        int num = userMapper.updateUser(user);
        if(num >  0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
}
